﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace SQLServerDAL
{
    public class Favorite:IDAL.IFavorite
    {
        /// <summary>
        /// 添加网址夹
        /// </summary>
        /// <param name="favorite">网址夹实体</param>
        /// <returns>受影响的行数</returns>
        public int AddFavorite(Model.Favorite favorite)
        {
            string sql = "insert into favorite values (@FName,@UID,@PID)";
            SqlParameter[] pars ={
                                    new SqlParameter ("@FName",favorite.FName),
                                    new SqlParameter("@UID",favorite.UID),
                                    new SqlParameter("@PID",favorite.PID)
                                 };
            return SqlHelp.ExecuteSql(sql, pars);
        }
        /// <summary>
        /// 通过用户查询其网址夹
        /// </summary>
        /// <param name="uid">用户编号</param>
        /// <returns>网址夹集合</returns>
        public IList<Model.Favorite> GetFavorite(int uid)
        {
            string sql = "select * from Favorite where uid=@UID";
            SqlParameter[] pars = { new SqlParameter("@UID", uid) };
            SqlDataReader dr = SqlHelp.ExecuteReader(sql, pars);
            IList<Model.Favorite> list = GetFavorite(dr);
            return list.Count > 0 ? list : null;
        }

        /// <summary>
        /// 查询用户最顶层网址夹
        /// </summary>
        /// <param name="uid">用户编号 </param>
        /// <returns>网址夹集合</returns>
        public IList<Model.Favorite> GetTopFavorite(int uid) {
            string sql = "select * from Favorite where uid=@UID and pid=0";
            SqlParameter[] pars = { new SqlParameter("@UID", uid) };
            SqlDataReader dr = SqlHelp.ExecuteReader(sql, pars);
            IList<Model.Favorite> list = GetFavorite(dr);
            return list.Count > 0 ? list : null;
        }

        /// <summary>
        /// 通过父网址夹查询子网址夹
        /// </summary>
        /// <param name="pid">父网址夹编号</param>
        /// <returns>网址夹集合</returns>
        public IList<Model.Favorite> GetChildFavorite(int pid)
        {
            string sql = "select * from Favorite where pid=@PID";
            SqlParameter[] pars = { new SqlParameter("@PID", pid) };
            SqlDataReader dr = SqlHelp.ExecuteReader(sql, pars);
            IList<Model.Favorite> list = GetFavorite(dr);
            return list.Count > 0 ? list : null;
        }
        /// <summary>
        /// 封装数据
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private IList<Model.Favorite> GetFavorite(SqlDataReader dr)
        {
            IList<Model.Favorite> list = new List<Model.Favorite>();
            while (dr.Read())
            {
                Model.Favorite favorite = new Model.Favorite();
                favorite.FID = dr.GetInt32(0);
                favorite.FName = dr.GetString(1);
                favorite.UID = dr.GetInt32(2);
                favorite.PID = dr.GetInt32(3);
                list.Add(favorite);
            }
            return list;
        }
    }
}
